//
//  AddressPickerView.swift
//  sss
//
//  Created by hrrMac on 2020/6/3.
//  Copyright © 2020 hrrMac. All rights reserved.
//

import UIKit

class AddressPickerView: MultiPickerView {
    ///默认到区
    var showType: AddressPickerType = .area
    ///省
    var provinceModelArr: [AddressProviceModel]?
    ///市
    var cityModelArr: [AddressCityModel]?
    ///区
    var areaModelArr: [AddressAreaModel]?
    
    ///地址选择器
    var pickerView:UIPickerView = {
        let pickerView = UIPickerView(frame: CGRect(x: 0, y: M_S_TOP_HEIGHT + 0.5, width: M_S_W, height: M_S_PICKER_HEIGHT))
        pickerView.backgroundColor = UIColor.white
        return pickerView
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        configPickerView()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func configPickerView(){
        alertView.addSubview(pickerView)
        pickerView.delegate = self
    }
}


///API
extension AddressPickerView{
    class func showAddressPicker(showType:AddressPickerType = .area){
        
    }
}



extension AddressPickerView:UIPickerViewDelegate,UIPickerViewDataSource{
    //有多少列
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        switch showType {
        case .provice:
            return 1
        case .city:
            return 2
        case .area:
            return 3
        }
    }
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        if component == 0 { //省
            return 1;
        }else if component == 1 {
            return 2;
        }else if component == 2 {
            return 3;
        }
        return 0;
    }
    
    
}

///数据处理
extension AddressPickerView{
    func loadData(){
        let path = Bundle.main.path(forResource: "city", ofType: "plist")
        let dataArr:NSArray = NSArray(contentsOfFile: path!)!
        let data:[Dictionary<String,Any>] =  dataArr as! [Dictionary<String,Any>]
        for dic in data {
            var model = AddressProviceModel()
            model.code = dic["code"] as? String
            model.name = dic["name"] as? String
            model.citylist = dic["citylist"] as? [Dictionary<String, Any>]
        }
    }
    
}




enum AddressPickerType {
    ///只显示省
    case provice
    ///显示省市
    case city
    ///显示省市区
    case area
}

///省
struct AddressProviceModel {
    var code:String?
    var name:String?
    var citylist:[Dictionary<String, Any>]?
    
}

///市
struct AddressCityModel {
    var code:String?
    var name:String?
    var arealist:[Dictionary<String, Any>]?
}

///区
struct AddressAreaModel {
    var code:String?
    var name:String?
}

